home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 234_01 / xdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  17.5 KB  |  700 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     XDIR - Hard Disk Manager
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Apr 03, 1987
  6.   DESCRIPTION:     Hard Disk Manager for IBM PC
  7.   KEYWORDS:     Hard Disk Manager Dump Directory
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      xdir.C
  10.   WARNINGS:     None
  11.   CRC:         N/A
  12.   SEE-ALSO:     HDIR.DOC and XDIR.DOC
  13.   AUTHOR:     Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
  14.   COMPILERS:     ECO-C
  15.   REFERENCES:     XDIR.DOC
  16. */
  17.  
  18.  
  19. /*
  20.    xdir.c
  21.    Superwash
  22.    to process the DOS directories
  23.  
  24. */
  25. #include "hdir.doc"          /* program documentation */
  26. #include "stdio.h"
  27. #include "dos.h"
  28. #include "dir.h"
  29. #include "xdir.h"
  30. struct tdir *pnam[MAXDIR];   /* directory entries */
  31. struct tdir *fnam[MAXFILE];  /* file names */
  32.  
  33. char  cdir[PATHSIZ];     /* current directory */
  34. char  helpfile[PATHSIZ] = "xdir.doc";      /* name of helpfile */
  35. unsigned cdate;          /* comparison date */
  36. /*
  37.   process DOS directories
  38.  
  39. */
  40. main(argc,argv)
  41. int  argc;     /* argument count */
  42. char **argv;   /* argument values */
  43. {
  44.      int    strcmp(char *,char *);
  45.      void   dironly(char **buffer, int type,int level);
  46.      void   update (char **buffer, int type,int level);
  47.      char   *strdsave(struct direct *, char *root);
  48.  
  49.      char   *alloc(int amt);
  50.      int    i,j;
  51.      char   answer[MAXFILE];      /* for responses */
  52.      char   srmask[15];           /* search mask */
  53.      int    att;                  /* attribute search */
  54.      struct tdir *tp;
  55.      static int pcount, fcount;
  56.      int    cf,ct;                /* flag for see files */
  57.      char   work[20];             /* work area selected dir */
  58.      static int firstime = 0;    /* for command line directory */
  59.      char   c,temp[20];             /* debug */
  60.  
  61.  
  62.      if (argc == 2)             /* process command line args */
  63.       {
  64.       if (strcmp(argv[1],"help") == 0) typ(helpfile);
  65.       if (strcmp(argv[1],"-v")   == 0) ;
  66.       }
  67.      else if (argc == 1)
  68.       {
  69.       i = help();               /* display help message */
  70.       if (i == 1) typ(helpfile);
  71.       }
  72.  
  73.      pcount = bildir();                   /* build path array */
  74.      qksort(pcount,pcomp,pswap);          /* sort by path name */
  75.  
  76.      if (argc == 2)                       /* test command line directory */
  77.      {
  78.      strcpy(work,argv[1]);
  79.      j = testdir(work,pcount);
  80.      if (j < 0) firstime = 0;
  81.         else
  82.          {
  83.          firstime = 1;
  84.          strcpy(cdir,work);
  85.          clear(answer,MAXFILE,' ');
  86.          answer[j] = 'S';
  87.          }
  88.      }
  89.  
  90.  
  91.      while (1)                         /* main loop */
  92.      {
  93.  
  94.      if (firstime)   firstime = 0;
  95.          else
  96.          {
  97.          clear(answer,MAXFILE,' ');   /* clear answers */
  98.          disx(pnam,pcount,answer,1); /* ask what directory  <=== */
  99.          }
  100.  
  101.      for (i=ct=0;i<MAXFILE;i++)        /* get directory selection */
  102.      {
  103.      if (answer[i] == 'S')   /* selection criteria */
  104.         {
  105.         ++ct;
  106.         tp = pnam[i];
  107.         strcpy(cdir,tp->d_name);       /* note current directory */
  108.         if (cdir[0] == 0) strcpy(cdir,"**ROOT**");
  109.         break;
  110.         }
  111.      }
  112.  
  113.      if (ct == 0) break;                 /* nothing selected */
  114.  
  115.      while (1)                           /* get search criteria */
  116.      {
  117.      j = criteria(srmask,&att,&cf);
  118.      if (j) break;
  119.      }
  120.  
  121.      fcount = bilfil(pnam[i],srmask,cdate,att); /* build file names array */
  122.      if (fcount == 0)
  123.         {
  124.         xmsg("No entries selected - press any key ");
  125.         continue;
  126.         }
  127.  
  128.      if (fcount == -1)
  129.         {
  130.         xmsg("Couldn't allocate memory ");
  131.         break;
  132.         }
  133.  
  134.      switch (cf)    /* determine sort method */
  135.      {
  136.      case 'D':
  137.        qksort(fcount,fcompd,fswap);
  138.        break;
  139.      case 'S':
  140.        qksort(fcount,fcomps,fswap);
  141.        break;
  142.      default:
  143.        qksort(fcount,fcomp,fswap);     /* sort alpha on file names */
  144.        break;
  145.      }
  146.  
  147.      if ((cf == 'Y') || (cf == 'D') || (cf == 'S') || (cf == 'A'))
  148.           dfnam(fcount,cdir);    /* display files */
  149.  
  150.      clear(answer,MAXFILE,' ');
  151.      disx(fnam,fcount,answer,2);   /* ask what files */
  152.      for (i=ct=0;i<MAXFILE;i++)    /* count responses */
  153.         if (answer[i] != ' ') ++ct;
  154.  
  155.      if (ct) action(answer,pcount);    /* process answer */
  156.      for (i=0;i<fcount;i++) free(fnam[i]);  /* free storage */
  157.  
  158.      clrscr();
  159.      cursor(5,10);
  160.      writestr("Processing completed - enter next directory name ");
  161.      cursor(6,10);
  162.      writestr("Press ENTER for root directory, ESC to end ");
  163.      c = getch();
  164.      if (c == 27) break;
  165.      if (c == 13) work[0] = 0;
  166.         else
  167.         {
  168.         putchar(c);
  169.         gets(temp);
  170.         work[0] = c;
  171.         work[1] = 0;
  172.         strcat(work,temp);
  173.         }
  174.  
  175.      j = testdir(work,pcount);
  176.      if (j < 0) firstime = 0;
  177.         else
  178.          {
  179.          firstime = 1;
  180.          strcpy(cdir,work);
  181.          clear(answer,MAXFILE,' ');
  182.          answer[j] = 'S';
  183.          }
  184.  
  185.      }   /* end while 1 processing */
  186.      clrscr();
  187. }
  188.  
  189. /*
  190.   action
  191.   take the requested action
  192. */
  193. void action(answer,pcount)
  194. char *answer;
  195. int  pcount;
  196. {
  197.  
  198.      static int  i,j,k,l,c;
  199.      int    rc;                 /* return code from delete */
  200.      char filename[80];         /* used in rename and copy */
  201.      struct tdir *tp, *fp;
  202.      int  row;
  203.      char key[30];              /* encryption key */
  204.      static int dflag;          /* flag for display */
  205.      int copybulk = 0;          /* no bulk copy */
  206.      char bulkname[80];         /* bulk file name */
  207.      int found;                 /* found flag for search of paths */
  208.      char workname[80];         /* work area for file name */
  209.  
  210.      dflag = 1;                 /* indicate need for screen display */
  211.  
  212.      for (i=0;i<MAXFILE;i++)
  213.      {
  214.      tp = fnam[i];
  215.      j = (int) answer[i];
  216.  
  217.      switch (j)
  218.      {
  219.      case 'T':                  /* list a file to console */
  220.        typ(tp->d_path);
  221.        dflag = 1;               /* indicate new display required */
  222.        break;
  223.      case 'L':                  /* list file to printer */
  224.        clrscr();
  225.        writestr("    File ");
  226.        writestr(tp->d_path);
  227.        writestr(" will be listed to the printer ");
  228.        lis(tp->d_path);
  229.        break;
  230.      case 'C':                  /* copy file */
  231.        if (copybulk)   /* bulk copy no questions */
  232.           {
  233.           strcpy(filename,bulkname);
  234.           strcat(filename,"\\");
  235.           strcat(filename,tp->d_name);
  236.           xcopy(tp->d_path,filename);
  237.           break;
  238.           }
  239.  
  240.           dscr();         /* display the screen */
  241.           row = 4;
  242.           cursor(row++,20);
  243.           writestr("Copy from file ");
  244.           writestr(tp->d_path);
  245.           writestr(" to ? ");
  246.           gets(filename);
  247.           if (strlen(filename) == 0) break;
  248.           for (l=0,k=strlen(filename);l<k;l++)
  249.               filename[l] = toupper(filename[l]);
  250.  
  251.           if (filename[0] != '\\')
  252.              {
  253.              cvp(tp->d_path,workname);
  254.              strcat(workname,"\\");
  255.              strcat(workname,filename);
  256.              strcpy(filename,workname);  /* build output name */
  257.              }
  258.  
  259.           else
  260.           {
  261.           for (l=found=0;l<pcount;l++)   /* see if a path */
  262.             {
  263.             fp = pnam[l];
  264.             k = strcmp(fp->d_path,filename);
  265.             if (k == 0) found = 1;
  266.             }
  267.  
  268.           if (found == 1)
  269.              {
  270.              cursor(row++,20);
  271.              writestr("Desire bulk copy? (Y)es (N)o ");
  272.              c = toupper(getch());
  273.              if (c == 'Y')
  274.                 {
  275.                 copybulk = 1;
  276.                 strcpy(bulkname,filename);
  277.                 strcat(filename,"\\");
  278.                 strcat(filename,tp->d_name);
  279.                 xcopy(tp->d_path,filename);
  280.                 }
  281.              }
  282.  
  283.            }
  284.  
  285.            xcopy(tp->d_path,filename);     /* copy the file */
  286.  
  287.        break;
  288.      case 'R':         /* rename the file */
  289.        dscr();         /* display the screen */
  290.        row = 4;
  291.        cursor(row,20);
  292.        writestr("Rename file ");
  293.        writestr(tp->d_path);
  294.        writestr(" to ? ");
  295.        gets(filename);
  296.        if (strlen(filename) == 0) break;
  297.        if (filename[0] != '\\')
  298.           {
  299.           cvp(tp->d_path,workname);
  300.           strcat(workname,"\\");
  301.           strcat(workname,filename);
  302.           strcpy(filename,workname);  /* build output name */
  303.           }
  304.        xrename(tp->d_path,filename);
  305.        break;
  306.      case 'D':            /* dump a file */
  307.        xdump(tp->d_path);
  308.        break;
  309.        dflag = 1;         /* indicate new display */
  310.      case 'X':            /* delete a file */
  311.        if (dflag) {dscr();dflag = 0;row=3;}
  312.        cursor(row++,20);
  313.        writestr("Delete file ");
  314.        writestr(tp->d_path);
  315.        writestr(" ? Enter 'Y' to delete ");
  316.        c = toupper(getch());
  317.        if (c == 'Y')
  318.           {
  319.           rc = unlink(tp->d_path);
  320.           if (rc == -1) xmsg("Unable to delete protected file");
  321.           }
  322.        if (row > 20) {dscr(); row =4;dflag=0;}
  323.        break;
  324.      case 'H':    /* hide the file */
  325.        upmod(tp->d_path,"h");
  326.        break;
  327.      case 'U':    /* unhide */
  328.        upmod(tp->d_path,"u");
  329.        break;
  330.      case 'P':    /* protect */
  331.        upmod(tp->d_path,"r");
  332.        break;
  333.      case 'W':    /* write - remove read-only status */
  334.        upmod(tp->d_path,"xa");
  335.        break;
  336.      case 'E':    /* encrypt */
  337.        dscr();         /* display the screen */
  338.        dflag = 1;      /* indicate new display is required */
  339.        row = 4;
  340.        cursor(row++,20);
  341.        writestr("Encrypt file ");
  342.        writestr(tp->d_path);
  343.        cursor(row++,20);
  344.        writestr("New name ");
  345.        gets(filename);
  346.        if (strlen(filename) == 0) break;
  347.        cursor(row++,20);
  348.        writestr("Encryption key ");
  349.        gets(key);
  350.        secure(tp->d_path,filename,key,1);   /* do the security */
  351.        break;
  352.      case 'S':         /* search for text */
  353.        ctlr(tp->d_path);
  354.        dflag = 1;            /* indicate new display */
  355.        break;
  356.      }   /* end case */
  357.      }   /* end for  */
  358.  
  359. }
  360.  
  361. /*
  362.      get the file selection criteria
  363.      criteria
  364.      return a 1 if criteria are ok, otherwise 0
  365.  
  366. */
  367. int criteria(srmask,att,cf)
  368. char *srmask;     /* search mask */
  369. int  *att;        /* attributes */
  370. int  *cf;         /* see files */
  371. {
  372.  
  373.      char c;
  374.      int  item, refresh;
  375.      char smask[20], satt[20], scf[20];
  376.      char sdate[20];
  377.      int  i;
  378.  
  379.      smask[0] = satt[0] = scf[0] = sdate[0] = item = 0;
  380.      refresh = 1;
  381.  
  382.      while (1)
  383.      {
  384.  
  385.      if (refresh)
  386.      {
  387.      clrscr();
  388.      boxx(79,24,1,1);
  389.      cursor(2,20);
  390.      writestr("Hard Disk Manager - File Selection Criteria ");
  391.      cursor(3,20);
  392.      writestr("Processing directory - ");
  393.      writestr(cdir);
  394.  
  395.      cursor(10,20);                  /* display questions */
  396.      writestr("List (A)lpha (D)ate (S)ize? [ESC=end] ");
  397.      cursor(10,60);
  398.      writestr(scf);
  399.      cursor(11,20);
  400.      writestr("After date YYMMDD? ");
  401.      cursor(11,50);
  402.      writestr(sdate);
  403.      cursor(12,20);
  404.      writestr("Search mask? [*.*]");
  405.      cursor(12,50);
  406.      writestr(smask);
  407.      cursor(13,20);
  408.      writestr("File attribute to search? ");
  409.      cursor(13,50);
  410.      writestr(satt);
  411.      cursor(14,20);
  412.      writestr("Above items OK? (Y,N) ");
  413.      cursor(20,20);
  414.      writestr("Press F1 for help");
  415.      refresh = 0;
  416.      }
  417.  
  418.      switch (item)
  419.      {
  420.      case 0:      /* list type */
  421.          cursor(10,60);                 /* get answers */
  422.          c = toupper(getch());
  423.          switch (c)
  424.          {
  425.          case 'A':
  426.          case 'D':
  427.          case 'S':
  428.          case 13:
  429.             if (c == 13) c = 'A';
  430.             putchar(c);
  431.             *cf = (int) c;
  432.             ++item;
  433.             break;
  434.          case 27:   /* escape */
  435.            *att = 0;    /* select all attributes */
  436.            cdate = 0;                  /* all dates */
  437.            strcpy(srmask,"*.*");       /* all masks */
  438.            *cf = 0;                    /* no display */
  439.            return (1);
  440.            break;
  441.          case 0:    /* extended character */
  442.            c = getch();
  443.          default:
  444.            xmsg("Enter sort sequence for file display or ESC = default");
  445.            break;
  446.          }         /* end switch c */
  447.          break;
  448.       case 1:     /* after date */
  449.         cursor(11,50);
  450.         i = gethelp(sdate);
  451.         if (i) xmsg("Select file by date, e.g. 860226 = Feb 26,1986");
  452.         else
  453.           {
  454.           cdate = cvdate(sdate);       /* convert to binary */
  455.           if (strlen(sdate) == 0)
  456.              {
  457.              cursor(11,50);
  458.              writestr("ALL");
  459.              }
  460.           ++item;
  461.           }
  462.         break;
  463.       case 2:         /* search mask */
  464.         cursor(12,50);
  465.         i = gethelp(smask);
  466.         if (i) xmsg("File search mask (e.g. *.bak) default *.*");
  467.         else
  468.         {
  469.         if (strlen(smask) == 0)
  470.            {
  471.            strcpy(srmask,"*.*");
  472.            cursor(12,50);
  473.            writestr(srmask);
  474.            }
  475.         else strcpy(srmask,smask);
  476.         ++item;
  477.         }
  478.         break;
  479.       case 3:         /* File attribute */
  480.         boxx(15,6,18,5);
  481.         cursor(19,6);
  482.         writestr("Attributes");
  483.         cursor(20,6);
  484.         writestr("(R)eadonly");
  485.         cursor(21,6);
  486.         writestr("(H)idden");
  487.         cursor(22,6);
  488.         writestr("(S)ystem ");
  489.         cursor(23,6);
  490.         writestr("(A)rchive ");
  491.  
  492.         cursor(13,50);
  493.         i = gethelp(satt);
  494.         if (i) xmsg("File attributes - default is all ");
  495.         else
  496.         {
  497.         if (strlen(satt) == 0)
  498.               {
  499.               *att = 0;
  500.               cursor(13,50);
  501.               writestr("ahdr");
  502.               }
  503.         else *att = cvatt(satt);  /* convert attributes */
  504.         ++item;
  505.         }
  506.         break;
  507.       case 4:
  508.         cursor(14,50);
  509.         c = toupper(getch());
  510.         if (c == 'Y') return 1;
  511.         if (c == 'N') return 0;
  512.         putchar(0x07);
  513.         xmsg("Enter Yes or No [Y,N]");
  514.         break;
  515.         }          /* end of switch */
  516.  
  517.     }     /* end while 1 */
  518. }         /* end function */
  519.  
  520. /*
  521.      dscr
  522.      display the screen for action
  523.  
  524. */
  525. void dscr()
  526. {
  527.     clrscr();
  528.     boxx(79,24,1,1);
  529.     cursor(2,20);
  530.     writestr("Hard Disk Manager - Selection Processing ");
  531.     return;
  532.  
  533. }
  534.  
  535. /*
  536.     fcompd
  537.     compare files by dates
  538.  
  539. */
  540. fcompd(i,j)
  541. int i,j;
  542. {
  543.    struct tdir *tpi;
  544.    struct tdir *tpj;
  545.  
  546.    tpi = fnam[i];
  547.    tpj = fnam[j];
  548.    if (tpi->d_date < tpj->d_date) return (1);
  549.    else
  550.    if (tpi->d_date > tpj->d_date) return (-1);
  551.    else
  552.    if (tpi->d_time < tpj->d_time) return (1);  /* same date diff time */
  553.    else
  554.    if (tpi->d_time > tpj->d_time) return (-1);
  555.    else return (0);
  556.  
  557. }
  558.  
  559.  
  560. /*
  561.     fcomps
  562.     compare files by size
  563.  
  564. */
  565. fcomps(i,j)
  566. int i,j;
  567. {
  568.    struct tdir *tpi;
  569.    struct tdir *tpj;
  570.  
  571.    tpi = fnam[i];
  572.    tpj = fnam[j];
  573.    if (tpi->d_size < tpj->d_size) return (-1);
  574.    else
  575.    if (tpi->d_size > tpj->d_size) return (1);
  576.    else return (0);
  577.  
  578. }
  579.  
  580. /*
  581.   help
  582.   display initial help message
  583.   return 1 if help is requested, otherwise 0
  584.  
  585. */
  586. help()
  587. {
  588.    int  i;
  589.  
  590.    clrscr();
  591.    boxx(79,24,1,1);
  592.    cursor(5,10);
  593.    writestr("XDIR - extended directory processing");
  594.    cursor(6,10);
  595.    writestr("version 1.0 ");
  596.    cursor(8,10);
  597.    writestr("The following options are available: ");
  598.    cursor(10,10);
  599.    writestr("F1 = Help ESC = end any other key = continue ");
  600.    cursor(11,10);
  601.    writestr("Note: to bypass this screen type xdir xxx ");
  602.    cursor(12,10);
  603.    writestr("  where xxx is any directory name");
  604.  
  605.    cursor(17,10);
  606.    writestr("Action to take? ");
  607.    cursor(17,27);
  608.    i = getch();
  609.    if (i == 27)
  610.       {
  611.       clrscr();
  612.       exit(0);
  613.       }
  614.  
  615.    if (i == 0) i = getch();
  616.    if (i == 59) return (1);
  617.       else return 0;
  618.  
  619. }
  620.  
  621. /*
  622.    xmsg
  623.    display a message at bottom, await reply then erase message
  624.  
  625. */
  626. void xmsg(msg)
  627. char *msg;
  628. {
  629.     int i,j;
  630.  
  631.     cursor(23,2);
  632.     writestr(msg);
  633.     i = getch();
  634.     if (i == 0) getch();
  635.     j = strlen(msg);
  636.     cursor(23,2);
  637.     for (i=0;i<j;i++) putchar(' ');
  638.  
  639. }
  640.  
  641. /*
  642.     gethelp
  643.     fill buffer
  644.     return 1 if help requested, otherwise 0
  645.  
  646. */
  647. gethelp(buffer)
  648. char *buffer;
  649. {
  650.       char temp[40], work[40];
  651.       int  c;
  652.  
  653.       c = getch();     /* first character */
  654.       if (c == 0)
  655.          {
  656.          c = getch();
  657.          if (c == 59) return (1);
  658.          }
  659.  
  660.       if (c == 13) {buffer[0] = 0;return 0;}
  661.  
  662.       putchar(c);
  663.       temp[0] = c;       /* any other character */
  664.       temp[1] = 0;
  665.       gets(work);
  666.       strcat(temp,work);
  667.       strcpy(buffer,temp);
  668.       return 0;
  669. }
  670.  
  671. /*
  672.       testdir
  673.       test that character string is a valid directory
  674.       return 0 if valid else -1
  675. */
  676. testdir(dirname,pcount)
  677. char *dirname;        /* name to test */
  678. int  pcount;          /* count of valid directory names */
  679. {
  680.      char work[20];
  681.      int  i,j;
  682.      char c, *cp;
  683.      struct tdir *tp;
  684.  
  685.      i = 0;
  686.      cp = dirname;
  687.      while (c = *cp++) work[i++] = toupper(c);  /* make upper case */
  688.      work[i] = 0;
  689.      strcpy(dirname,work);
  690.  
  691.      for (i=0;i<pcount;i++)               /* see if dir is valid */
  692.      {
  693.      tp = pnam[i];
  694.      j = strcmp(tp->d_name,work);
  695.      if (j == 0) break;
  696.      }
  697.      if (j == 0) return (i);
  698.         else return (-1);
  699. }
  700.